using System;
using System.Text;
using System.Runtime.InteropServices;
using Microsoft.Win32;

namespace CaveArt.Common.Util
{
    /// <summary>
    /// Common Functions and system parameters
    /// </summary>
    public class NetUtil
    {
        [DllImport("wininet.dll")]
        private extern static bool InternetGetConnectedState(out int Description, int ReservedValue);
        public static bool NetFailed = false;

        public NetUtil()
        {

        }

        private static bool MinMax(int val, int min, int max)
        {
            return (val >= min && val <= max);
        }

        public static bool ValidEmail(string[] strarr, int maxstring)
        {
            bool bRet = true;
            foreach (string str in strarr)
            {
                if (!ValidEmail(str, maxstring))
                {
                    bRet = false;
                    break;
                }
            }

            return bRet;
        }

        public static bool ValidEmail(string str, int maxstring)
        {
            bool bRet = false;

            if (MinMax(str.Length, 3, maxstring))		// 3 <= str <= max
            {
                int i = str.IndexOf('@');

                if (MinMax(i, 1, str.Length - 1))
                {
                    int count = 0;
                    char ch = '.';
                    bool BeforeAtSign = true;
                    bool PrevDot = false;
                    CharEnumerator c = str.GetEnumerator();

                    while (c.MoveNext())
                    {
                        ch = c.Current;

                        if (ch == '@')
                        {
                            count++;
                            BeforeAtSign = false;
                            if (count > 1)
                            {
                                bRet = false;
                                break;
                            }
                        }

                        if (!(Char.IsLetterOrDigit(ch) ||
                                ch.Equals('@') ||
                                (!PrevDot && ch.Equals('.')) ||
                                (!BeforeAtSign && ch.Equals('-')) ||
                                (BeforeAtSign && ch.Equals('_')))
                            || Char.IsWhiteSpace(ch))
                        {
                            bRet = false;
                            break;
                        }

                        PrevDot = ch.Equals('.');
                        bRet = true;
                    }

                    if (bRet)
                    {
                        bRet = !ch.Equals('.');
                    }
                }
            }

            return bRet;
        }
      
        public static bool IsInternetConnected()
        {
            int Desc;
            return InternetGetConnectedState(out Desc, 0);
        }
    }
}
